package com.acwing.partition9;

import java.io.*;
import java.util.LinkedList;

/**
 * @author `RKC`
 * @date 2021/12/1 12:30
 */
public class AC852spfa判断负环 {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] s = reader.readLine().split(" ");
        int n = Integer.parseInt(s[0]), m = Integer.parseInt(s[1]);
        int[][] edges = new int[m][3];
        for (int i = 0; i < m; i++) {
            s = reader.readLine().split(" ");
            int x = Integer.parseInt(s[0]), y = Integer.parseInt(s[1]), z = Integer.parseInt(s[2]);
            edges[i][0] = x;
            edges[i][1] = y;
            edges[i][2] = z;
        }
        if (spfa(n, edges)) writer.write("Yes\n");
        else writer.write("No\n");
        writer.flush();
    }

    private static boolean spfa(int n, int[][] edges) {
        int m = edges.length, index = 0;
        //判断负环就可以不初始化distance为最大值，因为是正数就不是负权，如果有负权才可能入队，且只有在一个到达一个点的路上经过了n个点，就必定存在负环
        int[] head = new int[n + 1], element = new int[m], next = new int[m], weight = new int[m], vertexCount = new int[n + 1];
        boolean[] inQueue = new boolean[n + 1];
        int[] distance = new int[n + 1];
        LinkedList<Integer> queue = new LinkedList<>();
        //因为判断图中是否有负环，需要对结点全部入队，因此inQueue直接初始化为true
        for (int i = 1; i <= n; i++) {
            head[i] = -1;
            inQueue[i] = true;
            queue.addLast(i);
        }
        for (int[] edge : edges) {
            element[index] = edge[1];
            next[index] = head[edge[0]];
            head[edge[0]] = index;
            weight[index++] = edge[2];
        }
        while (!queue.isEmpty()) {
            int u = queue.pollFirst();
            inQueue[u] = false;
            for (int i = head[u]; i != -1; i = next[i]) {
                int v = element[i];
                if (distance[v] > distance[u] + weight[i]) {
                    distance[v] = distance[u] + weight[i];
                    vertexCount[v] = vertexCount[u] + 1;
                    //图一共只有n个结点，达到v结点的过程中经过了n个结点，说明必定存在负权环
                    if (vertexCount[v] == n) return true;
                    if (inQueue[v]) continue;
                    queue.addLast(v);
                    inQueue[v] = true;
                }
            }
        }
        return false;
    }
}
